# Title: Lab 1 # Author: Justin Rasmussen # Date created: 2021-01-25 # Date edited: 2021-01-25 # Description: Measures of central tendency, basic data manipulation & visualization # Load packages library(tidyverse) library(lubridate) # Import data # Import data from NYT github nyt_st_url <- "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv" nyt_cnt <- read.csv(nyt_st_url, stringsAsFactors = FALSE) # Export locally to avoid slow internet issues write.csv(nyt_cnt, "nyt_cnt.csv") # Basic data manipulation #==============================================================================+ # The dplyr package uses a few important verbs to manipulate data: # Select #---------- # this lets you choose variables (columns) in your dataframe nyt_cnt_select <- select(nyt_cnt, date, county, state, cases, deaths) # sometimes it's more efficient to just drop the ones you don't want nyt_cnt_select <- select(nyt_cnt, -fips) # Mutate #-------- # mutate lets you create new variables easily nyt_cnt_mutate <- mutate(nyt_cnt_select, date = ymd(date)) # quick note here: logical operators in R are: # equal to: == # not equal to: != # greater than: > # greater than or equal to: >= # less than: < # less than or equal to: <= # not: ! # or: | # and: & # Filter #-------- # filter lets you choose observations (rows) in your dataframe nyt_cnt_filter <- filter(nyt_cnt_mutate, date > ymd("2020-12-01"), state == "North Carolina") # Summarize #---------- # summarize lets you create aggregate tables nyt_cnt_summarize <- summarize(nyt_cnt_filter, mean_cases = mean(cases), median_cases = median(cases), min_cases = min(cases), max_cases = max(cases)) # Group_by #---------- # group_by() lets you create grouped tables to summarize nyt_cnt_grouped <- group_by(nyt_cnt_filter, state_county) nyt_cnt_summarize <- summarize(nyt_cnt_grouped, mean_cases = mean(cases), median_cases = median(cases), min_cases = min(cases), max_cases = max(cases)) # Piping #-------- # this IS a pipe: %>% # a pipe allows you to pass the result of one function group_by(nyt_cnt_filter, county) %>% # on to the data argument of the next function summarize(mean_cases = mean(cases), median_cases = median(cases), min_cases = min(cases), max_cases = max(cases)) # you can also use . to represent the data being piped in: nyt_cnt_filter %>% group_by(., county) %>% summarize(., mean_cases = mean(cases), median_cases = median(cases), min_cases = min(cases), max_cases = max(cases)) # with a pipe we can perform data manipulation wihtout intermediate objects nyt_cnt_summarize <- nyt_cnt %>% select(-fips) %>% mutate(date = ymd(date)) %>% filter(date > ymd("2020-12-01"), state == "North Carolina") %>% group_by(county) %>% summarize(mean_cases = mean(cases), median_cases = median(cases), min_cases = min(cases), max_cases = max(cases), mean_deaths = deaths) # Basic data visualization #=============================================================================== # ggplot2 is a package that builds plots in layers nyt_cnt_filter %>% # mapping data to aesthetics ggplot(aes(y = cases, x = date, color = county)) + # choosing a geometry to represent those aesthetics geom_line() + # lots of other layers to play with e.g. theme_minimal() # you can click "Export" directly from the plot window... # or "Zoom" and then right-click to "Save as picture" # Challenge #=============================================================================== # Calculate the mean cases and deaths for 3 states of your choice in 2021 # Create a graph showing how deaths and cases have changed in each state